package com.liao.tensquare.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liao.tensquare.admin.dao.TbLogDao;
import com.liao.tensquare.admin.pojo.TbLog;
import com.liao.tensquare.admin.service.TbLogService;
import com.liao.tensquare.admin.vo.LogVO;
import com.liao.tensquare.admin.vo.ResLog;
import com.liao.tensquare.constants.LogConstant;
import com.liao.tensquare.enums.OperatorType;
import com.liao.tensquare.utils.DateUtils;
import com.liao.tensquare.utils.StringHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TbLogServiceImpl implements TbLogService {

    @Autowired
    private TbLogDao tbLogDao;

    @Override
    public LogVO findByPage(int pageSize, int currentPage, TbLog tbLog) {
        LogVO logVO = new LogVO();
        QueryWrapper<TbLog> queryWrapper = new QueryWrapper<>();
        Page<TbLog> tbLogPage = new Page<>(currentPage, pageSize);
//        queryWrapper.eq(!StringHelper.isEmpty(tbLog.getUserId()), "user_id", tbLog.getUserId());
        queryWrapper.eq(tbLog.getOperType() != null, "oper_type", tbLog.getOperType());
        queryWrapper.eq(tbLog.getStatus() != null, "status", tbLog.getStatus());
        queryWrapper.like(!StringHelper.isEmpty(tbLog.getUsername()), "username", tbLog.getUsername());
        //即使第一个参数是false，第三个也会执行，所以要注意空指针
        if (!StringHelper.isEmpty(tbLog.getSort())) {
            queryWrapper.orderBy(true, tbLog.getSort().equals("ascending"), "oper_time");
        }
        if (!StringHelper.isEmpty(tbLog.getOperTimes())) {
            String[] createDates = tbLog.getOperTimes().split("~");
            Date beginCreateDate = DateUtils.parse(DateUtils.FormatType.type1, createDates[0]);
            Date endCreateDate = DateUtils.parse(DateUtils.FormatType.type1, createDates[1]);
            queryWrapper.between("oper_time", beginCreateDate, endCreateDate);
        }
        tbLogDao.selectPage(tbLogPage, queryWrapper);
        logVO.setCurrentPage(tbLogPage.getCurrent());
        logVO.setPageSize(tbLogPage.getSize());
        logVO.setTotal(tbLogPage.getTotal());
        List<TbLog> records = tbLogPage.getRecords();
        logVO.setTbLogs(records);
        return logVO;
    }

    @Override
    public void saveLog(TbLog tbLog) {
        tbLogDao.insert(tbLog);
    }

    @Override
    public void del(ArrayList<Integer> ids) {
        tbLogDao.deleteBatchIds(ids);
    }

    @Override
    public void clear() {
        tbLogDao.delete(null);
    }

    @Override
    public List<List<String>> export() {
        List<TbLog> tbLogs = tbLogDao.selectList(null);
        return getExcelData(tbLogs);
    }

    private List<List<String>> getExcelData(List<TbLog> tbLogs) {
        List<List<String>> lists = new ArrayList<>();
        for (TbLog tbLog : tbLogs) {
            List<String> list = new ArrayList<>();
            list.add(String.valueOf(tbLog.getId()));
            list.add(tbLog.getUserId());
            list.add(tbLog.getUsername());
            list.add(LogConstant.getOerTypeStr(tbLog.getOperType()));
            list.add(tbLog.getContent());
            list.add(String.valueOf(tbLog.getCostTime()));
            list.add(tbLog.getMethod());
            list.add(tbLog.getRequestType());
            list.add(tbLog.getUrl());
            list.add(LogConstant.getOperStatus(tbLog.getStatus()));
            list.add(tbLog.getErrorMsg());
            list.add(tbLog.getResult());
            list.add(DateUtils.format(DateUtils.FormatType.type2, tbLog.getOperTime()));
            lists.add(list);
        }
        return lists;
    }


}
